package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.activity.QuanXainOrganizationActivity
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.ManagerViewPageAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.ProjectWarningActivityDelegate
import cc.vv.btpartyjob.partymanager.fragment.ProjectQueryFragment_YJ
import cc.vv.btpartyjob.partymanager.fragment.ProjectQueryFragment_YQ
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 项目警告
 * Created by Chen.qi on 2018/10/29.
 */
class ProjectWarningActivity : BaseActivityMVP<ProjectWarningActivityDelegate>() {

    /**
     * ViewPager适配器
     */
    private var mAdapter: ManagerViewPageAdapter? = null

    /**
     * Fragment集合
     */
    private var mFragments: ArrayList<Fragment>? = null

    private var arrState = listOf("1", "2")

    private var keyStr: String? = null

    private var pos: Int = 0


    override fun initData() {
        viewDelegate?.setTopBarTitle("项目预警")
        mFragments = ArrayList()
        val yq = ProjectQueryFragment_YQ()
        val yj = ProjectQueryFragment_YJ()
        mFragments!!.add(yq)
        mFragments!!.add(yj)
        mAdapter = ManagerViewPageAdapter(supportFragmentManager, mFragments!!)
        viewDelegate?.setAdapterData(mAdapter!!)
        viewDelegate?.setTextStatus(0)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                //项目搜索
//                if (viewDelegate?.getSearchShow()!!) {
//                    viewDelegate?.showSearchView(false)
//                } else {
//                    viewDelegate?.showSearchView(true)
//                }
                val intent = Intent(this@ProjectWarningActivity, QuanXainOrganizationActivity::class.java)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 0)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_SHIELD_UNIT, false)
                startActivityForResult(intent, 100)

//                when {
//                    UserRoleServer.getAreaRole() -> JniStartActivity.startIntentOrganActivity(this@ProjectWarningActivity, 0, 0, 100)
//                    UserRoleServer.getStreetRole() -> JniStartActivity.startIntentOrganActivity(this@ProjectWarningActivity, 1, 1, 100)
//                    UserRoleServer.getSheQuRole() -> JniStartActivity.startIntentOrganActivity(this@ProjectWarningActivity, 2, 2, 100)
//                    UserRoleServer.getGridRole() -> JniStartActivity.startIntentOrganActivity(this@ProjectWarningActivity, 3, 3, 100)
//                }
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //过期项目
            viewDelegate?.setViewPagerPos(0)


        }, R.id.tv_ipw_projectPastDul)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //预警项目
            viewDelegate?.setViewPagerPos(1)


        }, R.id.tv_ipw_projectWarning)


        viewDelegate?.bindViewPagerListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                viewDelegate?.setTextStatus(position)
                pos = position
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //空事件
            viewDelegate?.showSearchView(false)
        }, R.id.rl_ipw_searchTop)


//        viewDelegate?.setOnClickListener(View.OnClickListener {
//            //搜索
//            closeInput()
//            keyStr = viewDelegate?.getInputSearchText()
//            val fragment: ProjectWarningFragment
//            try {
//                fragment = mFragments!![pos] as ProjectWarningFragment
//                fragment.search(keyStr)
//            } catch (e: Exception) {
//            }
//
//            viewDelegate?.showSearchView(false)
//        }, R.id.tv_ipw_searchBtn)


    }

    override fun getDelegateClass(): Class<ProjectWarningActivityDelegate> {
        return ProjectWarningActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val communityId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val type = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE)
            try {
                val yj = mFragments!![0] as ProjectQueryFragment_YQ
                val yq = mFragments!![1] as ProjectQueryFragment_YJ
                yj.zuZhiQuery(communityId, type)
                yq.zuZhiQuery(communityId, type)
            } catch (e: Exception) {
            }
        }

    }


}